package com.iptv.grape.domain;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.ReadJsonFile;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// 节目单预约列表
public class BillAppointmentContainer {

    public static final String SaveFileName = "bllAppointments.json";

    private List<BillAppointment> billAppointmentList = new ArrayList<BillAppointment>();

    public List<BillAppointment> getBillAppointmentList() {
        return billAppointmentList;
    }

    public synchronized void initData(){
        List<BillAppointment> tmpList = new ArrayList<BillAppointment>();
        for(int i = 0; i < billAppointmentList.size(); i++){
            if(!billAppointmentList.get(i).isCancel()){
                tmpList.add(billAppointmentList.get(i));
            }
        }
        this.billAppointmentList = tmpList;
        serializeToFile();
    }

    // 构造函数
    public BillAppointmentContainer() {

    }

    // 添加一个直播预约对象
    public void add(Channel channel, ChannelBill channelBill){
        BillAppointment billAppointment = new BillAppointment(channel,channelBill);
        billAppointmentList.add(billAppointment);
        serializeToFile();
    }

    // 设置预约对象的取消状态
    public void setState(Channel channel, ChannelBill channelBill) {
        int index = getIndex(channel,channelBill);
        if(index >= 0){
            billAppointmentList.get(index).setCancel(!billAppointmentList.get(index).isCancel());
            channelBill.setAppointment(!billAppointmentList.get(index).isCancel());
        }
        serializeToFile();
    }

    // 根据直播频道对象和节目单对象来删除预约列表中的预约对象
    public void remove(Channel channel, ChannelBill channelBill){
        int index = getIndex(channel,channelBill);
        if(index >= 0){
            billAppointmentList.remove(index);
            channelBill.setAppointment(false);
            //serializeToFile();
            //AppLog.getCurLog().out("删除了预约一个对象");
        }
    }

    // 判断是否已经被预约
    public boolean isExists(Channel channel, ChannelBill channelBill){
        return getIndex(channel,channelBill) > -1;
    }

    // 根据直播频道对象和节目单对象来查询预约列表中的下标
    public int getIndex(Channel channel, ChannelBill channelBill){
        int index = -1;
        for(int i = 0; i < billAppointmentList.size(); i++){
            BillAppointment billAppointment = billAppointmentList.get(i);
            if(billAppointment.getChannel().getKeyID().equals(channel.getKeyID()) &&
                    billAppointment.getChannelBill().getKeyID().equals(channelBill.getKeyID())){
                index = i;
                break;
            }
        }
        return index;
    }

    // 将列表转化为json
    public String toJson(){
        //Gson gson = new Gson();
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        String json = gson.toJson(this.billAppointmentList);
        AppLog.getCurLog().out("预约列表转Json=" + json);
        return json;
    }

    // 序列化到文件
    public void serializeToFile(){
        String json = this.toJson();
        ReadJsonFile.writeFile(SaveFileName,json);
        AppLog.getCurLog().out("序列化预约列表到文件");
    }

    // 反序列化对象
    public static BillAppointmentContainer unSerializeToFile(){
        BillAppointmentContainer billAppointmentContainer = new BillAppointmentContainer();
        try {
            if(ReadJsonFile.fileExists(SaveFileName)) {
                Type type = new TypeToken<List<BillAppointment>>() {}.getType();
                String json = ReadJsonFile.readFile(SaveFileName);
                Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
                List<BillAppointment> tmpList = gson.fromJson(json, type);
                if(tmpList != null && tmpList.size() > 0){
                    for(int i = 0; i < tmpList.size(); i++){
                        BillAppointment billAppointment = tmpList.get(i);
                        boolean isOK = billAppointment.init();
                        if(isOK && !billAppointment.isEmpty()){
                            billAppointmentContainer.billAppointmentList.add(billAppointment);
                        }
                    }
                }
            }
            return billAppointmentContainer;
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        return billAppointmentContainer;
    }
}
